from java.awt import Color
from java.awt import Font
from java.lang import System
from java.lang import String
from java.lang import Double
from java.lang import Math
from java.util import Locale
from java.io import File

from hep.aida import *

from jhplot.jadraw import JaKey
from jhplot.jadraw import JaText
from jhplot  import F1D
from jhplot  import HPlotJa


#from fr.in2p3.ipnl.aidautilities import *

from jscinttools.scint.emissionspectrum import ModEmSpec
from jscinttools.utilities.aidautilities import *
from jscinttools.utilities.scintutilities import *
from jscinttools.utilities.jhplotutilities import *

Locale.setDefault(Locale.ENGLISH)
af = IAnalysisFactory.create();

showFigures = False
writeFigures = not showFigures

""" Define colors and fonts"""
figType = [".png", ".eps"]
colors = [Color.BLUE, Color.RED, Color(0, 170, 0), Color.BLACK, Color.GRAY, Color.CYAN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.GREEN, Color.DARK_GRAY]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 25)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)

outPath = "/home/marc-antoine/Dropbox/ZnWO4/"
outputFile = "ZnWO4CoincEff"

numPhot = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0]
tau = 200.0
effFunc = []
func = []
funcKey = []
funcRange = [0.001, 100]

for i in range(len(numPhot)):
	effFunc.append(af.createFunctionFactory(af.createTreeFactory().create()).createFunctionFromScript("Coincidence Efficiency", 1, "1-exp(-1.0*x[0]*n*n/tau)", "n, tau", "Coincidence Efficiency"))
	effFunc[i].setParameter("n", numPhot[i]);
	effFunc[i].setParameter("tau", tau);
	#func.append(F1D(String.valueOf(numPhot[i]).split(".0")[0]+" Phot per Chan", effFunc[i], funcRange[0], funcRange[1], points=100000, color=colors[i], penWidth=2))
	func.append(F1D(String.valueOf(numPhot[i]).split(".0")[0]+" Phot", effFunc[i], funcRange[0], funcRange[1], points=100000, color=colors[i], penWidth=2))
	funcKey.append(JaKey(20, func[i].getTitle() , 0.7, 0.3+i*0.05, "NDC", color=func[i].getColor(), keyColor=func[i].getColor(), font=ticFont))
tauText = JaText("&tau; = "+String.valueOf(tau).split(".0")[0]+" &micro;s", 0.2, 0.175, "NDC", color=colors[0], font=legendFont)


########################################################################
# Plot
########################################################################
""" Create the Plotter"""
plotter = HPlotJa("Coincidence Efficiency in ZnWO4", 600, 600, 1, 1, True)
plotter.setGTitle("Coincidence Efficiency in ZnWO_{4}", titleFont)
plotter.antiAlias = False
plotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
plotter.setRange(funcRange[0], funcRange[1], 0, 1.1)
plotter.setLogScale(0, True)
plotter.setLogScale(1, False)
plotter.ticFont = ticFont
plotter.showStatBox = False
plotter.showKey(False)
plotter.nameX = "Coincidence Window (&micro;s)"
plotter.nameY = "Efficiency"

""" Plots all the JHep objects"""
for i in range(len(numPhot)):
	plotter.draw(func[i])
	plotter.draw(funcKey[i])
plotter.draw(tauText)

""" Modifies the JaAxes and the axes labels"""
axes = plotter.getPad()
axes.setLabelSpace(0, 0.0)
axes.setLabelSpace(1, 0.0)
HPlotJaUtil.setTickLabelOneToPower(0, axes)
HPlotJaUtil.setTickLabelZeroPositive(axes)

labX = plotter.getLabelX()[0][0]
labX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX, plotter, 0.025)
labY = plotter.getLabelY()[0][0]
labY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY, plotter, -0.00)
 
plotter.update()

""" Saves or not the plot"""
if showFigures:
	plotter.visible(True)
else:
	plotter.visible(False)	
	for i in range(len(figType)):
		outFileName = outputFile + figType[i]
		plotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
plotter.update()
